import 'package:dio/dio.dart';
import 'package:package_info/package_info.dart';

import '../../config/ApiConfig.dart';
import '../../util/LoadingUtils.dart';
import '../../util/cache_util.dart';
import '../../util/string_util.dart';


//Option拦截器可以用来统一处理Option信息 可以在这里添加
class OptionInterceptor extends InterceptorsWrapper {

  String packageName = "";
  String currentVersion = "";

  OptionInterceptor(){
   initData();
  }

  initData() async {
    var packageInfo = await PackageInfo.fromPlatform();
    packageName = packageInfo.packageName;
    currentVersion = packageInfo.version;
  }

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) async{

    print("onRequest : ${options.path}");

    ///表示默认 请求是 展示loading
    if (options.extra["hideLoading"] != true) {
      LoadingUtils.loading();
    }

    /// 添加token，不是登录接口在添加
    if(options.path != ApiConfig.login){
      options.headers["satoken"] = ApiConfig.getToken;
    }
    print("onRequest : ${ApiConfig.getToken}");
    // options.headers["packageName"] = packageName;
    // options.headers["version"] = currentVersion;

    String? mainUrl = CacheUtil().get<String>(ApiConfig.mainUrl);
    //表示可以外边设置登录URL，缓存时候设置
    if (StringUtil.isNotEmpty(mainUrl)) {
      options.baseUrl = mainUrl!;
    } else {
      options.baseUrl = ApiConfig.mainUrl;
    }

    handler.next(options);
  }

}
